/* eslint-disable no-unused-vars */
import {
  app,
  Menu,
  BrowserWindow,
  ipcMain,
  dialog,
  nativeImage,
} from 'electron';
import fs from 'fs';

let mainWindows:BrowserWindow;
function createWindows() {
  Menu.setApplicationMenu(null);
  mainWindows = new BrowserWindow({
    width: 1200,
    height: 800,
    title: 'mod 汉化工具',
    resizable: true,
    show: false,
    frame: false,
    transparent: false,
    icon: './electron/icon.png',
    titleBarStyle: 'hidden',
    webPreferences: {
      nodeIntegration: true,
    },
  });

  mainWindows.loadURL('http://localhost:8000');
  // mainWindows.loadURL("file://" + __dirname + "/index.html");
  mainWindows.webContents.openDevTools();
  mainWindows.on('ready-to-show', () => {
    mainWindows.maximize();
    mainWindows.show();
  });
  mainWindows.on('close', () => {
    app.quit();
  });
}

app.on('ready', createWindows);
app.on('window-all-closed', () => {
  app.quit();
});

//ipcRender Event
ipcMain.on('close', (_e: any) => {
  mainWindows.close();
});
ipcMain.on('max', (_e: any) => {
  if (mainWindows.isMaximized()) {
    mainWindows.unmaximize();
  } else {
    mainWindows.maximize();
  }
});
ipcMain.on('min', (_e: any) => {
  mainWindows.minimize();
});
ipcMain.on('selectFolder', (e: any) => {
  dialog
    .showOpenDialog(mainWindows, {
      title: '选择项目',
      filters: [{ name: 'ProjectJson', extensions: ['json'] }],
      properties: ['openFile'],
    })
    .then((v: any) => {
      e.reply('projectRoot', v);
    });
});

ipcMain.on('addFile', (e: any) => {
  dialog
    .showOpenDialog(mainWindows, {
      title: '选择文件',
      filters: [{ name: 'ProjectFile', extensions: ['json'] }],
      properties: ['openFile'],
    })
    .then((v: any) => {
      e.reply('projectAddFile', v);
    });
});
